19. Exercise: Add Firebase to your Project

L1 A19 Add Firebase To Your Project

Resources

Step 1: Create a Firebase project

Before you can add Firebase to your Android app, you need to create a Firebase project to connect to your Android app.

  1. After logging in to the Firebase console, click Add project, then select or enter a Project name. Name your project ‘fcm-codelab’.

  2. Click Continue.

  3. You can skip setting up Google Analytics and choose the ‘Not Right Now’ option.

  4. Click ‘Create Project’ to finish setting up the Firebase project.

Step 2: Register your app with Firebase

Now that you have a Firebase project, you can add your Android app to it.

  1. In the center of the Firebase console's project overview page, click the Android icon to launch the setup workflow.

  2. Enter the package id of your android project, com.example.android.eggtimernotifications, as the Firebase application Id. Make sure you enter the ID your app is using because you cannot add or modify this value after you’ve registered your app with your Firebase project.

  3. Click Register app.

Step 3: Add the Firebase configuration file to your project

Add the Firebase Android configuration file to your app:

  1. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).

    • You can download your Firebase Android config file again at any time.
    • Make sure the config file is not appended with additional characters and should only be named google-services.json
  2. Move your config file into the module (app-level) directory of your app.

Step 4: Configure your Android project to enable Firebase products

  1. To enable Firebase products in your app, add the google-services plugin to your Gradle files.

  2. In your (project-level) Gradle file (build.gradle), add rules to include the Google Services plugin. Check that you have Google's Maven repository, as well.

build.gradle

buildscript {

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }

  dependencies {
    // ...

    // Add the following line:
    classpath 'com.google.gms:google-services:4.3.0'  // Google Services plugin
  }
}

allprojects {
  // ...

  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    // ...
  }
}
  1. In your app-level Gradle file (usually app/build.gradle), add a line to the bottom of the file.

app/build.gradle

apply plugin: 'com.android.application'

android {
  // ...
}

// Add the following line to the bottom of the file:
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin
  1. Since the gradle files are changed, perform a gradle sync.